{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Info:执行函数1\n",
      "Info:执行函数2\n"
     ]
    }
   ],
   "source": [
    "#load \"LoadBaseTool.csx\"\n",
    "using BaseTool;\n",
    "\n",
    "Action mAction = null;\n",
    "\n",
    "public void Function1()\n",
    "{\n",
    "    BaseLog.SendLog(\"执行函数1\", LogLevel.Info);\n",
    "}\n",
    "\n",
    "public void Function2()\n",
    "{\n",
    "    BaseLog.SendLog(\"执行函数2\", LogLevel.Info);\n",
    "}\n",
    "\n",
    "mAction += Function1;\n",
    "mAction += Function2;\n",
    "\n",
    "mAction?.Invoke();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Info:当前数值为：10\n",
      "Warning:数值发生了改变...\n",
      "Info:当前数值为：11\n"
     ]
    }
   ],
   "source": [
    "class ValueChanged<ValueType>\n",
    "{\n",
    "    private ValueType mValue;\n",
    "    public Action OnMyValueChanged;\n",
    "\n",
    "    public ValueChanged(ValueType _init_value)\n",
    "    {\n",
    "        mValue = _init_value;   // 设置初始值\n",
    "    }\n",
    "\n",
    "    public void SetValue(ValueType _new_value)\n",
    "    {\n",
    "        if (!mValue.Equals(_new_value))\n",
    "        {\n",
    "            mValue = _new_value;\n",
    "            OnMyValueChanged?.Invoke();\n",
    "        }\n",
    "    }\n",
    "\n",
    "    public override string ToString()\n",
    "    {\n",
    "        return $\"{mValue}\";\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "// 测试代码\n",
    "public void ValueChangedFunction()\n",
    "{\n",
    "    BaseLog.SendLog(\"数值发生了改变...\", LogLevel.Warning);\n",
    "}\n",
    "\n",
    "ValueChanged<int> _variable = new ValueChanged<int>(10);\n",
    "_variable.OnMyValueChanged += ValueChangedFunction;\n",
    "BaseLog.SendLog($\"当前数值为：{_variable}\", LogLevel.Info);\n",
    "_variable.SetValue(11);\n",
    "BaseLog.SendLog($\"当前数值为：{_variable}\", LogLevel.Info);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "Action _action1 = () => Console.WriteLine(\"Action 1 executed\");\n",
    "Action _action2 = () => Console.WriteLine(\"Action 2 executed\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Action 1 executed\n",
      "Action 2 executed\n"
     ]
    }
   ],
   "source": [
    "// 使用 Delegate.Combine 合并两个 Action\n",
    "Action _combine1 = (Action)Delegate.Combine(_action1, _action2);\n",
    "_combine1();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Action 1 executed\n",
      "Action 2 executed\n"
     ]
    }
   ],
   "source": [
    "Action _combine2 = _action1 + _action2;\n",
    "_combine2();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果Action是null也是没问题的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Action 1 executed\n",
      "Action 2 executed\n",
      "Action 1 executed\n",
      "Action 2 executed\n"
     ]
    }
   ],
   "source": [
    "Action _action3 = null;\n",
    "Action _combine3 = (Action)Delegate.Combine(_action1, _action2, _action3);\n",
    "Action _combine4 = _action1 + _action2 + _action3;\n",
    "\n",
    "_combine3();\n",
    "_combine4();"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "csharp"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
